package org.jin.samples.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.poi.ss.usermodel.Workbook;
import org.jin.samples.dao.IManageStudentDao;
import org.jin.samples.model.ExamRole;
import org.jin.samples.model.ExamUser;
import org.jin.samples.model.vo.ExamUserVO;
import org.jin.samples.service.IManageStudentService;
import org.jin.samples.utils.ExcelUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ManageStudentService implements IManageStudentService{
	
	@Autowired
	private IManageStudentDao ManageStudentDao;
	
	@Override
	public List<ExamUser> findAllUser(){
		return ManageStudentDao.find("FROM ExamUser");		
	}
	
	@Override
	public ExamUserVO findByIdUser(String id){
		ExamUser entity;
		try {
			entity = ManageStudentDao.getUserById(id);
			if(entity != null){
				ExamUserVO vo = new ExamUserVO();
				BeanUtils.copyProperties(entity, vo);
				
				return vo;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;
	}
	
	@Override
	public void create(ExamUserVO vo){
		ExamUser entity = new ExamUser();
		BeanUtils.copyProperties(vo, entity);
		ManageStudentDao.add(entity);
	}
	
	@Override
	public void Updata(ExamUserVO vo){
		ExamUser user = ManageStudentDao.get(vo.getUserId());
		if(user != null){
			System.out.println(user.getUserId());
			vo.setLastLockTime(user.getLastLockTime());
			vo.setPassword(user.getPassword());
			vo.setLoginFailCount(user.getLoginFailCount());
			BeanUtils.copyProperties(vo, user);
			ManageStudentDao.saveOrUpdate(user);
		}
	}
	
	@Override
	public void UpdataBy(ExamUserVO vo){
		ExamUser user = ManageStudentDao.get(vo.getUserId());
		if(user != null){
			BeanUtils.copyProperties(vo, user);
			ManageStudentDao.saveOrUpdate(user);
		}
	}
	
	@Override
	public void delete(String userId){
		ExamUser user = ManageStudentDao.get(userId);
		if(user != null){
			ManageStudentDao.delete(user);
		}
	}
	
	public void saveStudentByExcel(InputStream inputStream, String postfix){
		Workbook wk = ExcelUtils.openWorkbook(inputStream, postfix);
		List<ExamUserVO> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		List<ExamUser> entities = new ArrayList<ExamUser>();
		if(allExcelUser != null){
			//ExamRole role = examRoleDAO.get(25);
			ExamUser entity = null;
			for(int i =0; i<allExcelUser.size();i++){
				entity = new ExamUser();
				BeanUtils.copyProperties(allExcelUser.get(i), entity);
				//Set<ExamRole> examRoles = new HashSet<ExamRole>();
				//examRoles.add(role);
				//entity.setExamRoles(examRoles);
				entities.add(entity);
			}
		}
		ManageStudentDao.save(entities);
	}
	
}
